<template>
	<view>
		<u-popup :show="show" :round="10" @close="close">
			<view class="popupTitle">
				<u-icon name="close" color='#fff'></u-icon>
				<view>
					选择摄像头
				</view>
				<u-icon name="close" @click="close"></u-icon>
			</view>
			<view class="popupBody">
				<view style="flex: 1;">
					<z-paging ref="paging" :fixed="false" v-model="data" @query="query" :default-page-no="1"
						:default-page-size="10">
						<view class="BodyBox" v-if="!loading">
							<view class="bodyItem" v-for="(item,index) in data" :key="item" @click="choose(item.id)"
								:class="{choose:ids.includes(item.id)}">
								<view class="bodyHeader">
									<view class="icon-shexiangtou iconfont font40"></view>
									<view>
										<u-text lines='1' :text='`序列号${item.serial_number}`' size='24rpx' />
									</view>
								</view>
								<view>
									<text style="color: #bbb;">{{item.name?item.name:'暂无设备名称'}}</text>
								</view>
							</view>
						</view>
						<view class="BodyBox" v-else>
							<view class="bodyItem Skeleton" v-for="(item,index) in 20" :key="item" />
						</view>
					</z-paging>
				</view>
				<u-button v-if="ids.length" :disabled="disabled" @click='add'>
					添加
				</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		props: ['show', 'decorate_id'],
		data() {
			return {
				loading: true,
				data: [],
				ids: [],
				disabled: false
			}
		},
		watch: {},
		methods: {
			choose(id) {
				if (this.ids.includes(id)) {
					this.ids = this.ids.filter(item => item != id)
				} else {
					this.ids.push(id)
				}
			},
			close() {
				this.$emit('close')
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api_company.Device.List(obj).then(res => {
					this.$refs.paging.complete(res.data.devices.data)
					this.loading = false
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
				})
			},
			add() {
				this.disabled = true
				this.$api_company.Decorate.Device.Add(this.decorate_id, {
					device_ids: this.ids
				}).then(res => {
					uni.$u.toast('添加成功')
					this.close()
					this.$emit('reload')
					this.disabled = false
				}).catch(err => {
					uni.$u.toast(err.message)
					this.disabled = false
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.BodyBox {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		align-items: flex-start;
	}

	.popupBody {
		width: 100vw;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.bodyHeader {
		display: flex;
		justify-content: space-between;
	}

	.bodyItem {
		padding: 40rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		flex-direction: column;
		width: 49%;
		border-radius: 8rpx;
		background-color: #eee;
		height: 200rpx;
		margin-bottom: 20rpx;
	}

	.font40 {
		font-size: 40rpx;
	}

	.icon-guanji- {
		color: #bbb;
	}

	.blue {
		color: #3975c6;
	}

	.choose {
		border: 1rpx solid #3975c6;
	}
</style>